using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

/**
*@file       LSAccount_LoginReciever.cs
*@brief      process message LSAccount_Login
*
*@author     graywind
*@version    1.0.0.1
*@data       2012/4/14
*/

//process LSAccount_Login
public class OnLSAccount_Login
{
    /// <summary>
    /// process message
    /// </summary>
    /// <param name="op">message operator</param>
    /// <param name="session_id">session identifier</param>
    /// <param name="msg_buffer">message buffer</param>
    /// <param name="msg_bytes">buffer length</param>
    public static void OnMapping(IMessageOperator op, Int32 session_id, byte[] msg_buffer, UInt32 msg_bytes)
    {
        //TODO
		loginserver.UsrAccountInfo account_info = ProtoNet<loginserver.UsrAccountInfo>.CreateFromBytes(msg_buffer, msg_bytes);
        if (account_info.UsrName != string.Empty)
        {
            int error = -1;
            int login_code;

            do
            {
                if (!op.LoginManager.Add(account_info.UsrName, session_id, out login_code))
                {
                    error = (int)dbserver.LoginFailed.eErrorCode.Account_Already_Online;
                    break;
                }

                if (login_code == UsrLoginManager.INVALID_CODE)
                {
                    error = (int)dbserver.LoginFailed.eErrorCode.Max_Player_Arrived;
                    break;
                }

                msgDBAccount_Login msg = new msgDBAccount_Login(session_id, account_info.UsrName, account_info.UsrPassword, login_code);
                gwMessagePort.ProcessMessage(msg);

            } while (false);

            if (error >= 0)
            {
                msgLSAccount_Login_Failed msg = new msgLSAccount_Login_Failed(session_id, (dbserver.LoginFailed.eErrorCode)error);
                gwMessagePort.ProcessMessage(msg);
            }
        }
        else
        {
            System.Console.WriteLine("Empty User Name From Session:{0}", session_id);
            NetLib.close_session(session_id);
        }
    }
}

//register callback
partial class MessageProcTypes<ProcType, MessageProc>
{
        [MessageProcType((int)LoginServerTypes.MessageType.LSAccount_Login)]
        public static LoginServerProcessor.LoginServerTabFunc LSAccount_LoginProc = new LoginServerProcessor.LoginServerTabFunc(OnLSAccount_Login.OnMapping);
}
